//
//  InspectionInputCustomCell.swift
//  Employee
//
//  Created by swordjoy on 2017/4/24.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

enum InspectionInputCustomType {
  case date
  case pullMenu
}

class InspectionInputCustomCell: UITableViewCell {
  
  static var identifier = "InspectionInputCustomCell.swordjoy"
  
  var name: String = "" {
    
    didSet { titleLabel.text = name }
    
  }
  
  var content: String = "" {
    
    didSet {
      inputLabel.text = content
      if content.isEmpty {
        inputLabel.textColor = GlobalConfig.TextLightColor
        if .date == type {
          inputLabel.text = "点击选择日期"
        } else {
          inputLabel.text = "点击选择菜单"
        }
      } else {
        inputLabel.textColor = GlobalConfig.BGSkyBlueColor
      }
    }
    
  }
  
  var options: [Option] = [] {
    didSet {
      let titles = options.map ({ return $0.name })
      sameLevelPicker?.titless = [titles]
    }
  }
  
  var isFlow: Bool = false {
    didSet {
      if isFlow {
        contentV.snp.updateConstraints({ (make) in
          make.left.right.top.bottom.equalToSuperview()
        })
      }
    }
  }
  
  var getSelect: ( (String, String) -> () )?
  
  var type: InspectionInputCustomType = .date {
    didSet {
      if .date == type {
        inputLabel.text = "点击选择日期"
      }
      else if .pullMenu == type {
    
        inputLabel.text = "点击选择下拉菜单"
        
        sameLevelPicker = SJSameLevelPicker()
        sameLevelPicker?.title = "请选择"
        sameLevelPicker?.delegate = self
        sameLevelPicker?.leftTitle = "清除"
      }
    }
  }
  
  fileprivate var contentV: UIView!
  
  fileprivate var titleLabel: UILabel!
//  fileprivate var textView: PlaceholderTextView!
  fileprivate var inputLabel: UILabel!
  
  fileprivate var sameLevelPicker: SJSameLevelPicker?
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    backgroundColor = UIColor.clear
    selectionStyle = .none
    
    create()
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
}

// MARK: - InspectionInputCustomCell

extension InspectionInputCustomCell: SJSameLevelPickerDelegate {
  func sameLevelPickerDidSelect(result: String, index: Int) {
    if result.isEmpty {
      inputLabel.text = "点击选择下拉菜单"
      self.inputLabel.textColor = GlobalConfig.TextLightColor
      getSelect?(self.name, "")
    }
    else {
      self.inputLabel.text = result
      self.inputLabel.textColor = GlobalConfig.BGSkyBlueColor
      getSelect?(self.name, result)
    }
  }
}

// MARK: - action

extension InspectionInputCustomCell {
  
  @objc fileprivate func chooseAction() {
    SJApp.endEdit()
    if .date == type {
      let dateV = SJDatePickerView(style: SJDatePickerStyle())
      dateV.getDatePickerDate = { dateString in
        if ""  == dateString {
          self.inputLabel.text = "点击选择日期"
          self.inputLabel.textColor = GlobalConfig.TextLightColor
        }
        else {
          self.inputLabel.text = dateString
          self.inputLabel.textColor = GlobalConfig.BGSkyBlueColor
        }
        self.getSelect?(self.name, dateString)
      }
      dateV.show()
    }
    else if .pullMenu == type {
      if options.isEmpty { return }
      sameLevelPicker?.show(in: SJApp.keyWindow)
    }
    
  }
  
}

// MARK: - fileprivate

extension InspectionInputCustomCell {

  fileprivate func create() {
    
    contentV = UIView()
    contentView.addSubview(contentV)
    contentV.backgroundColor = UIColor.white
    contentV.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.top.equalTo(5)
      make.bottom.equalTo(-5)
    }
    contentV.layer.cornerRadius = 4
    contentV.clipsToBounds = true
    
    titleLabel = UILabel()
    contentV.addSubview(titleLabel)
    titleLabel.font = GlobalConfig.system16Font
    titleLabel.textColor = GlobalConfig.TextDeepColor
    titleLabel.numberOfLines = 0
    titleLabel.snp.makeConstraints { (make) in
      make.left.top.equalTo(20)
      make.right.equalTo(-20)
    }
    
    inputLabel = UILabel()
    inputLabel.backgroundColor = UIColor.white
    contentV.addSubview(inputLabel)
    inputLabel.font = GlobalConfig.system15Font
    inputLabel.textColor = GlobalConfig.TextLightColor
    inputLabel.numberOfLines = 0
    inputLabel.snp.makeConstraints { (make) in
      make.top.equalTo(titleLabel.snp.bottom).offset(20)
      make.left.equalTo(40)
      make.right.equalTo(-40)
      make.height.equalTo(50)
    }
    
    inputLabel.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(chooseAction))
    inputLabel.addGestureRecognizer(tap)
    
    
  }
}
